package mock.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import mock.dto.StockDTO;
import mock.utility.Utility;

public class StockDAOImp implements StockDAO {

	private Utility util = new Utility();

	/**
	 * input: atmID
	 * output: stock List
	 */
	@Override
	public List<StockDTO> getStockByATMID(int atmID) {
		// TODO Auto-generated method stub
		Connection con = util.createConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = con.prepareStatement("select * from Stock where ATMID = ?");
			ps.setInt(1, atmID);
			rs = ps.executeQuery();
			List<StockDTO> list = new ArrayList<>();
			while (rs.next()) {
				int stockID = rs.getInt("StockID");
				int moneyID = rs.getInt("MoneyID");
				int ATMID = rs.getInt("ATMID");
				int quantity = rs.getInt("Quantity");
				StockDTO stock = new StockDTO(stockID, moneyID, ATMID, quantity);
				list.add(stock);
			}
			if (!list.isEmpty())
				return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			if (con != null)
				try {
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if (ps != null)
				try {
					ps.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if (rs != null)
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		return null;
	}

	/**
	 * input: atmID update stock
	 */
	@Override
	public boolean updateStockByATMIDAndQuantityAndMoneyID(int ATMID, int moneyID,
			int quantity) {
		// TODO Auto-generated method stub
		// create connection
		Connection con = util.createConnection();
		// declare preparedstatement
		if (con != null) {
			PreparedStatement ps = null;
			try {
				ps = con.prepareStatement("update Stock set Quantity = ? where ATMID = ? and MoneyID = ?");

				ps.setInt(1, quantity);
				ps.setInt(2, ATMID);
				ps.setInt(3, moneyID);

				int test = ps.executeUpdate();
				if (test >= 1)
					return true;
				else
					return false;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
				System.out.println("Update fail!");
				return false;
			} finally {
				if (con != null)
					try {
						con.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				if (ps != null)
					try {
						ps.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			}
		} else {
			System.out.println("Connection fail!");
			return false;
		}
		// execute
		// close connection and preparedstatement
	}

	/**
	 * input: atmID, moneyID
	 * output: stockDTO
	 */
	@Override
	public StockDTO getStockByATMIDAndMoneyID(int atmID, int moneyID) {
		// TODO Auto-generated method stub
		Connection con = util.createConnection();
		if(con != null){
			PreparedStatement ps = null;
			ResultSet rs = null;
			try {
				ps = con.prepareStatement("select * from Stock where ATMID = ? and MoneyID = ?");
				ps.setInt(1, atmID);
				ps.setInt(2, moneyID);
				rs = ps.executeQuery();
				if(rs.next()){
					int stockid = rs.getInt("StockID");
					int moneyid = rs.getInt("MoneyID");
					int atmid = rs.getInt("ATMID");
					int quantity = rs.getInt("Quantity");
					StockDTO stock = new StockDTO(stockid, moneyid, atmid, quantity);
					return stock;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				System.out.println("Can't not execute query string!");
			}finally{
				if(con != null)
					try {
						con.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				if(ps != null)
					try {
						ps.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				if(rs != null)
					try {
						rs.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			}
		}
		return null;
	}
	
}
